﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;
using log4net;
using ThreeByte.ActiveDeck.AddIn.Config;

namespace ThreeByte.ActiveDeck.Config
{
    public class ScoreReportConfig : PageConfig
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ScoreReportConfig));

        #region Public Properties

        private ObservableCollection<PageConfig> _questions = new ObservableCollection<PageConfig>();
        public ObservableCollection<PageConfig> Questions {
            get {
                return _questions;
            }
            set {
                _questions = value;
                NotifyPropertyChanged("Questions");
            }
        }

        #endregion Public Properties


        public ScoreReportConfig() {
            LayoutOrientation = Content.Orientation.Portrait; //By Default
        }

        private List<PageConfigRef> _pageRefs;

        public ScoreReportConfig(XElement node, Dictionary<Guid, GraphicResource> resources)
            : base(node, resources) {

                _pageRefs = new List<PageConfigRef>();
                foreach(XElement n in node.Elements(ns + "PageRef")) {
                    _pageRefs.Add(new PageConfigRef(int.Parse(n.Attribute("Section").Value), int.Parse(n.Attribute("Page").Value)));
                }

        }

        public override XElement ToXml() {
            XElement node = base.ToXml();

            foreach(PageConfig p in Questions) {
                XElement pageNode = new XElement(ns + "PageRef", new XAttribute("Section", p.ParentSlideConfig.SlideId), new XAttribute("Page", p.PageId));
                node.Add(pageNode);
            }

            return node;
        }

        public override void Refresh() {
            base.Refresh();
            try {
                //This should only happen once
                if(_pageRefs != null){
                    //Resolve the Page refs to actual pageconfig objects
                    foreach(PageConfigRef r in _pageRefs) {
                        if(ParentSlideConfig.Parent.ContainsPageId(r.Page)) {
                            PageConfig page = ParentSlideConfig.Parent[ParentSlideConfig.Parent.GetSectionForPageId(r.Page)][r.Page];
                            Questions.Add(page);
                        }
                    }

                    _pageRefs.Clear();
                    _pageRefs = null;
                }
            } catch(Exception ex) {
                log.Warn("Cannot Refresh the ScoreReport", ex);
            }
        }


        class PageConfigRef
        {
            public int Section { get; private set; }
            public int Page { get; private set; }

            public PageConfigRef(int section, int page) {
                Section = section;
                Page = page;
            }

            public PageConfigRef(PageConfig pageConfig) {
                Section = pageConfig.ParentSlideConfig.SlideId;
                Page = pageConfig.PageId;
            }
        }
    }
}
